package org.chenliang.emulator.ui;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class AboutDialog extends Dialog implements TextResource{

	public AboutDialog(Shell parent, int style) {
		super(parent, style);
	}

	public AboutDialog(Shell parent) {
		super(parent);
	}
	
	public void open(){
		final Display display = getParent().getDisplay();
		
		final Shell shell = new Shell(display , SWT.APPLICATION_MODAL | SWT.CLOSE | SWT.RESIZE);
		shell.setText(TEXT_ABOUT_TITLE);
		shell.setLayout(new FormLayout());
		
		FormData layoutData = new FormData();
		layoutData.left = new FormAttachment(0 , 0);
		layoutData.top = new FormAttachment(0 , 0);
		layoutData.right = new FormAttachment(100 , 0);
		layoutData.bottom = new FormAttachment(100 , 0);
		
		Text text = new Text(shell , SWT.READ_ONLY | SWT.WRAP /*| SWT.H_SCROLL*/ | SWT.V_SCROLL | SWT.NO_BACKGROUND);
		text.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
	
		InputStream is = this.getClass().getResourceAsStream("/README");
		if(is != null){
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String line = null;
			try {
				while((line = br.readLine()) != null){
					text.append(line + '\n');
				}
				br.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		text.setLayoutData(layoutData);
		
		layoutData = new FormData();
		layoutData.left = new FormAttachment(0 , 0);
		layoutData.top = new FormAttachment(text , 0 , SWT.BOTTOM);
		layoutData.right = new FormAttachment(100 , 0);
		layoutData.bottom = new FormAttachment(100 , 0);
		
		Rectangle r = display.getBounds();
		shell.setSize(r.width / 2, r.height / 2);
		shell.layout();
		shell.open();
		
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();	
			}
		}
	}
}
